<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="w-[310px]">
          <a-input v-model:value="formState.zjm" placeholder="物品编号/物品名称/助记码" />
        </a-form-item>
        <a-form-item name="month" label="核算月度" class="w-[310px] ml-3">
          <a-select v-model:value="formState.month" placeholder="请选择" :options="options" :field-names="{ label: 'month', value: 'month' }" allow-clear />
        </a-form-item>
        <a-form-item name="showZeroProject" label="显示0元项目" class="ml-3">
          <a-switch v-model:checked="formState.showZeroProject" @change="getData" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <a-space class="flex justify-end mb-2">
        <BaseExport type="物资" name="收发存查询" code="000169" :search-params="formState">导出</BaseExport>
      </a-space>
      <BaseTable :loading="loading" column-code="000169" :data-source="tableData" :pagination="pagination" bordered fill>
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            <div>{{ index + 1 }}</div>
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record.mc, gg: record.gg, cj: record.cj }" />
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onHandleEdit(record)"> 收发存明细 </a-button>
            <a-button type="link" size="small" @click="onPrint(record)"> 打印 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="onSuceess" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit'

const rowData = ref(null)
const editOpen = ref(false)
const options = ref([])
const formRef = ref(null)
const formState = ref({
  showZeroProject: false
})
const getCalculateData = async () => {
  const { code, data, msg } = await post('/materials/comprehenive/getMonthWeightedAverageList', {})
  if (code !== 0) return message.error(msg)
  options.value = data
}

const { loading, tableData, getData, pagination } = useTable({
  url: '/materials/comprehenive/getDrugMonthBillList',
  immediate: true,
  params: formState.value
})

const onHandleEdit = (row) => {
  rowData.value = row
  editOpen.value = true
}
// 打印
const onPrint = async (record) => {
  const { code, msg, data } = await post('/materials/comprehenive/printMtrDrugMonthBill', { month: record.month, mtrType: 0 })
  if (code !== 0) return message.error(msg || '获取打印数据失败')
  window.openExe?.rpt(data) //调用winform打印
}

onMounted(() => {
  getCalculateData()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}
</style>
